#!/bin/sh
#################################################
###  功能说明:原始数据导入
###  导入方式:全量导入
###  导入结果:hive中raw库
###  运行频率:初始化
###  数据来源:教学资源
###  运行条件:无,支持数据重跑
###  运行命令:sh job_name.sh
###  维护人:王梁
###  运行日期：2018-12-19
#################################################
HIVE_DB=raw
TABLE_PREFIX='te_'
if [ $# == 1 ]
    then
    echo "table:$1"
    tables=($1)
else
    tables=('
school_calendar
oc
organization
admin_major
teaching_class
resources
user
paper
exercise
ken
course
achievement_test
achievement_online_forum_qa
class_student
class_teacher
oc_forum
    ')

fi

for ele in ${tables[*]}
do
    table=${ele}

    #删除外部文件
    hadoop fs -rm -r "hdfs:/${HIVE_DB}/${TABLE_PREFIX}${table}"
    #删除表结构
    hive -e "USE ${HIVE_DB};DROP TABLE IF EXISTS ${TABLE_PREFIX}${table};"
    #新建表
   sqoop create-hive-table --connect jdbc:mysql://172.16.98.92:3306/ocedu --username ocedu_zg --password sxnyzy123! \
     --table ${table}  --hive-table ${HIVE_DB}.${TABLE_PREFIX}${table}
    #改为外部表
    hive -e "alter table ${HIVE_DB}.${TABLE_PREFIX}${table} set TBLPROPERTIES ('EXTERNAL'='TRUE');"
    #修改外部表路径
    hive -e "alter table ${HIVE_DB}.${TABLE_PREFIX}${table} set location 'hdfs:/${HIVE_DB}/${TABLE_PREFIX}${table}';"
    #导入数据
   sqoop import --hive-import --connect jdbc:mysql://172.16.98.92:3306/ocedu --username ocedu_zg --password sxnyzy123! \
    --table ${table} --hive-table ${HIVE_DB}.${TABLE_PREFIX}${table} \
    -m 1 --hive-overwrite \
    --input-null-string '\\N' \
    --input-null-non-string '\\N' \
    --hive-drop-import-delims \
    --null-string '\\N' --null-non-string '\\N' --fields-terminated-by '\0001'
            --input-fields-terminated-by '\001' --lines-terminated-by '\n' --input-null-string '\\N' --input-null-non-string '\\N' \

done

rm -rf ./*.java